package com.deyuanyun.pic.settings.controller.base;

import com.deyuanyun.pic.settings.domain.base.Code;
import com.deyuanyun.pic.settings.service.base.CodeService;
import com.github.pagehelper.Page;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * ztree demo
 *
 * @author xiatao
 * @datetime 2015年8月10日 13点29分50秒
 */
@Controller
@RequestMapping("/codeController")
public class CodeController {
    private static Logger log = LogManager.getLogger(CodeController.class);
    @Resource
    private CodeService codeService;

    @RequestMapping("/excelExport")
    public ModelAndView excelExport(HttpServletResponse response) {
        String filePath = "C:\\Users\\xtt\\Desktop\\15水工保护信息表（2张）.xls";
        List<String[]> list = new ArrayList<String[]>();
        for (int i = 0; i < 50000; i++) {
            String[] string = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16","17","18","19","20","21","22","23"};
            list.add(string);
        }
        List<List<String[]>> all=new ArrayList<List<String[]>>();
        all.add(list);
        int[] lineStart=new int[]{5};
//        ExcelUtils.jxlExportExcel(filePath,lineStart,"水工保护",all,response);
        return null;
    }

    @RequestMapping("/toCodeMgr")
    public ModelAndView toSysBaseCode() {
        log.error("进入了toSysBaseCode");
        ModelAndView mv = new ModelAndView();
        mv.setViewName("settings/code/codeMgr");
        return mv;
    }

    @RequestMapping("/selectList")
    public ModelAndView selectList() {
        Code code = new Code();
        ModelAndView mv = new ModelAndView();
        Page<Code> list = codeService.selectList(code);
        List<HashMap<String, Object>> list1 = new ArrayList<HashMap<String, Object>>();
        for (Code s : list) {
            HashMap<String, Object> hashMap = new HashMap<String, Object>();
            hashMap.put("id", s.getId());
            hashMap.put("pId", s.getParentId());
            hashMap.put("name", s.getName());
            hashMap.put("idPath", s.getIdPath());
            list1.add(hashMap);
        }
        mv.addObject("znodes", list1);
        return mv;
    }

    @RequestMapping("/addSysBaseCode")
    public ModelAndView addSysBaseCode(Code code) {
        log.error("进入了addSysBaseCode");
        ModelAndView mv = new ModelAndView();
        code.setId(UUID.randomUUID().toString());
        code.setIdPath(code.getIdPath() + code.getId() + "_");
        Integer result = codeService.insertSelective(code);
        mv.addObject("result", result);
        return mv;
    }


    @RequestMapping("/deleteSysBaseCode")
    public ModelAndView deleteSysBaseCode(String idPath) {
        ModelAndView mv = new ModelAndView();
        int result = codeService.deleteByIdPath(idPath);
        mv.addObject("result", result);
        return mv;
    }

    @RequestMapping("/updateSysBaseCode")
    public ModelAndView updateSysBaseCode(Code code) {
        ModelAndView mv = new ModelAndView();
        Integer result = codeService.updateByPrimaryKeySelective(code);
        mv.addObject("result", result);
        return mv;
    }

    @RequestMapping("/selectByIdPath")
    public ModelAndView selectByIdPath(String idPath) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("list", codeService.selectByIdPath(idPath));
        return mv;
    }
}
